package com.lzw.mall.admin.controller;


import com.lzw.mall.admin.entity.User;
import com.lzw.mall.admin.service.IUserService;
import com.lzw.mall.admin.util.Constants;
import com.lzw.mall.admin.util.RequestUtil;
import com.lzw.mall.common.vo.LayuiTableVO;import com.lzw.mall.admin.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lizhenwei
 * @since 2023-03-17
 */
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private IUserService userService;


    @GetMapping("/index")
    public String page(){
        return "user";
    }

    @GetMapping("/list")
    @ResponseBody
    public LayuiTableVO list(@RequestParam Integer page, @RequestParam Integer limit, String keyword,String sex){
        return userService.list(page, limit, keyword,sex);
    }

    @ResponseBody
    @PostMapping("/save")
    public ResultVO save(User u) {
        return userService.doSave(u);
    }


    @ResponseBody
    @PostMapping("/delete")
    public ResultVO delete(Integer id) {
        return userService.delete(id);
    }

    @PostMapping("/deletes")
    @ResponseBody
    public void deleteUser(@RequestParam("ids[]") Integer[] ids ){
        //System.out.println(Arrays.toString(ids));
        userService.deletes(ids);
    }

    @ResponseBody
    @GetMapping("/selectById")
    public ResultVO selectById(Integer id){
        return userService.selectById(id);
    }

    @ResponseBody
    @PostMapping("/avatar")
    public ResultVO avatar(MultipartFile file){
        ResultVO vo=new ResultVO();
        String fileName=System.currentTimeMillis() + ".png";
        File f=new File("D:\\test01\\user\\"+fileName);
        try {
            file.transferTo(f);
        } catch (IOException e) {
            e.printStackTrace();
        }
        Map<String,String> u = new HashMap();
        u.put("avatar",Constants.UPLOAD_PATH+"user\\"+fileName);
        vo.setCode(0);
        vo.setData(u);
        return vo;
    }

    @GetMapping("/selectByUsername")
    @ResponseBody
    public ResultVO selectByUsername(){
        User u=new User();
        u.setUsername(RequestUtil.getCurrent().getUsername());
        return userService.selectByUsername(u.getUsername());
    }

    @GetMapping("/password")
    public String p(){
        return "user-password";
    }

    @PostMapping("/updatePassword")
    @ResponseBody
    public ResultVO updatePassword(String oldPassword,String newPassword,String againPassword){
        System.out.println(oldPassword);
        return userService.updatePassword(oldPassword,newPassword,againPassword);
    }
    @PostMapping("/updateMima")
    @ResponseBody
    public ResultVO updateMima(Integer id){
        return userService.updateMima(id);
    }
}
